package com.qg.cyapp.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class HttpUtil {
	
	public static String doPost(String url, Map<String, String> paramsList)
			throws Exception {
		// 建立post连接和参数
		HttpPost httpRequest = new HttpPost(url);
		Log.e("url", url);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		for (String key : paramsList.keySet()) {
			params.add(new BasicNameValuePair(key, paramsList.get(key)));
		}
		//params.add(new BasicNameValuePair("pmh_xuan", userString));
		// 设置请求头和编码
		httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
		Log.i("param", "发送  " + params.toString());

		BasicHttpParams httpParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 6000);// 设置请求超时的时间
		HttpConnectionParams.setSoTimeout(httpParams, 6000); // 设置获取数据超时的时间
		HttpClient httpClient = new DefaultHttpClient(httpParams);

		// 获取响应
		HttpResponse httpResponse = httpClient.execute(httpRequest);

		if (httpResponse.getStatusLine().getStatusCode() == 200) {
			String result = EntityUtils.toString(httpResponse.getEntity());
			Log.i("postResponse", "postResponse--------" + result);
			return result;
		}

		return null;
	}
	
}
